package com.itheima.web.controller.system;

import com.github.pagehelper.PageInfo;
import com.itheima.domain.system.Dept;
import com.itheima.utils.BeanUtil;
import com.itheima.web.BaseServlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/system/dept")
public class DeptServlet extends BaseServlet {

    /**
     * 分页查询的方法
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String size = req.getParameter("size");
        String page = req.getParameter("page");
        PageInfo<Dept> all = deptService.findAll(size, page);
        req.setAttribute("page", all);
        req.getRequestDispatcher( "/WEB-INF/pages/system/dept/list.jsp").forward(req, resp);
    }

    /**
     * 跳转到添加界面
     *
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //加载所有的部门信息放入到deptList
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);
        //跳转到添加界面
        req.getRequestDispatcher( "/WEB-INF/pages/system/dept/add.jsp").forward(req, resp);
    }

    /**
     *  添加的方法
     * @param req
     * @param resp
     * @throws IOException
     */
    public void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dept dept = BeanUtil.fillBean(req, Dept.class, "yyyy-MM-dd");
        deptService.save(dept);
        //从定向到分页查询页面
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
    }

    /**
     *  跳转到修改界面,并将数据回显的方法
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    public void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //首先获取到需要修改的信息
        String id = req.getParameter("id");
        Dept dept =  deptService.findById(id);
        //请求转发到修改的jsp界面
        req.setAttribute("dept",dept);
        //加载所有的部门信息回显到修改界面
        List<Dept> all = deptService.findAll();
        req.setAttribute("deptList",all);
        req.getRequestDispatcher("/WEB-INF/pages/system/dept/update.jsp").forward(req,resp);
    }

    /**
     *  修改部门信息的方法
     * @param req
     * @param resp
     * @throws IOException
     */
    public void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Dept dept = BeanUtil.fillBean(req, Dept.class, "yyyy-MM-dd");
        deptService.update(dept);
        //修改完之后重定向到分页查询界面
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
    }

    public void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("id");
        deptService.delete(id);
        //删除完毕之后重定向到分页查询界面
        resp.sendRedirect(req.getContextPath()+"/system/dept?operation=list");
    }
}
